using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VQCKDZ
    {
        public DataRow Convert2QCKDZ(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryQCKDZ(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["QCKMANDANT"] = " ".PadRight(2 ); 
         dwReturn["QCKNUMB"] = " ".PadRight(8 ); 
         dwReturn["QCKAENZL"] = " ".PadRight(0 ); 
         dwReturn["QCKAENB"] = " ".PadRight(8 ); 
         dwReturn["QCKAEND"] = " ".PadRight(0 ); 
         dwReturn["QCKNEUB"] = " ".PadRight(8 ); 
         dwReturn["QCKNEUD"] = " ".PadRight(0 ); 
         dwReturn["QCKLIEFSCH"] = " ".PadRight(8 ); 
         dwReturn["QCKLIEFSCHPOS"] = " ".PadRight(3 ); 
         dwReturn["QCKAUFTYP"] = " ".PadRight(1 ); 
         dwReturn["QCKAUFKRS"] = " ".PadRight(2 ); 
         dwReturn["QCKAUFLFD"] = " ".PadRight(6 ); 
         dwReturn["QCKAUFPOS"] = " ".PadRight(3 ); 
         dwReturn["QCKTEILNR"] = " ".PadRight(4 ); 
         dwReturn["QCKKUNDNR"] = " ".PadRight(8 ); 
         dwReturn["QCKQORDER"] = " ".PadRight(9 ); 
         dwReturn["QCKDUSER"] = " ".PadRight(8 ); 
         dwReturn["QCKDRKZ"] = " ".PadRight(1 ); 
         dwReturn["QCKDRDAT"] = " ".PadRight(0 ); 
         dwReturn["QCKLAYOUT"] = " ".PadRight(8 ); 
         dwReturn["QCKFILLER"] = " ".PadRight(49 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapQCKDZ(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveQCKDZ(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryQCKDZ(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapQCKDZ(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertQCKDZ(dwLocal, ITXDBVERSION);
            }
            UpdateQCKDZ(dwLocal);
        }
        public void InsertQCKDZ(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO QCKDZ(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryQCKDZ(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM QCKDZ WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM QCKDZ  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "QCKDZ";
            Return = Result;
            return Return;
        }
        public void DeleteQCKDZ(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM QCKDZ   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateQCKDZ(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE QCKDZ SET "
                    +"QCKMANDANT= :QCKMANDANT,"
                    +"QCKNUMB= :QCKNUMB,"
                    +"QCKAENZL= :QCKAENZL,"
                    +"QCKAENB= :QCKAENB,"
                    +"QCKAEND= :QCKAEND,"
                    +"QCKNEUB= :QCKNEUB,"
                    +"QCKNEUD= :QCKNEUD,"
                    +"QCKLIEFSCH= :QCKLIEFSCH,"
                    +"QCKLIEFSCHPOS= :QCKLIEFSCHPOS,"
                    +"QCKAUFTYP= :QCKAUFTYP,"
                    +"QCKAUFKRS= :QCKAUFKRS,"
                    +"QCKAUFLFD= :QCKAUFLFD,"
                    +"QCKAUFPOS= :QCKAUFPOS,"
                    +"QCKTEILNR= :QCKTEILNR,"
                    +"QCKKUNDNR= :QCKKUNDNR,"
                    +"QCKQORDER= :QCKQORDER,"
                    +"QCKDUSER= :QCKDUSER,"
                    +"QCKDRKZ= :QCKDRKZ,"
                    +"QCKDRDAT= :QCKDRDAT,"
                    +"QCKLAYOUT= :QCKLAYOUT,"
                    +"QCKFILLER= :QCKFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("QCKMANDANT", OracleType.NVarChar).Value = dwData["QCKMANDANT"].ToString(); 
          or1.Parameters.Add("QCKNUMB", OracleType.NVarChar).Value = dwData["QCKNUMB"].ToString(); 
          or1.Parameters.Add("QCKAENZL", OracleType.NVarChar).Value = dwData["QCKAENZL"].ToString(); 
          or1.Parameters.Add("QCKAENB", OracleType.NVarChar).Value = dwData["QCKAENB"].ToString(); 
          or1.Parameters.Add("QCKAEND", OracleType.NVarChar).Value = dwData["QCKAEND"].ToString(); 
          or1.Parameters.Add("QCKNEUB", OracleType.NVarChar).Value = dwData["QCKNEUB"].ToString(); 
          or1.Parameters.Add("QCKNEUD", OracleType.NVarChar).Value = dwData["QCKNEUD"].ToString(); 
          or1.Parameters.Add("QCKLIEFSCH", OracleType.NVarChar).Value = dwData["QCKLIEFSCH"].ToString(); 
          or1.Parameters.Add("QCKLIEFSCHPOS", OracleType.NVarChar).Value = dwData["QCKLIEFSCHPOS"].ToString(); 
          or1.Parameters.Add("QCKAUFTYP", OracleType.NVarChar).Value = dwData["QCKAUFTYP"].ToString(); 
          or1.Parameters.Add("QCKAUFKRS", OracleType.NVarChar).Value = dwData["QCKAUFKRS"].ToString(); 
          or1.Parameters.Add("QCKAUFLFD", OracleType.NVarChar).Value = dwData["QCKAUFLFD"].ToString(); 
          or1.Parameters.Add("QCKAUFPOS", OracleType.NVarChar).Value = dwData["QCKAUFPOS"].ToString(); 
          or1.Parameters.Add("QCKTEILNR", OracleType.NVarChar).Value = dwData["QCKTEILNR"].ToString(); 
          or1.Parameters.Add("QCKKUNDNR", OracleType.NVarChar).Value = dwData["QCKKUNDNR"].ToString(); 
          or1.Parameters.Add("QCKQORDER", OracleType.NVarChar).Value = dwData["QCKQORDER"].ToString(); 
          or1.Parameters.Add("QCKDUSER", OracleType.NVarChar).Value = dwData["QCKDUSER"].ToString(); 
          or1.Parameters.Add("QCKDRKZ", OracleType.NVarChar).Value = dwData["QCKDRKZ"].ToString(); 
          or1.Parameters.Add("QCKDRDAT", OracleType.NVarChar).Value = dwData["QCKDRDAT"].ToString(); 
          or1.Parameters.Add("QCKLAYOUT", OracleType.NVarChar).Value = dwData["QCKLAYOUT"].ToString(); 
          or1.Parameters.Add("QCKFILLER", OracleType.NVarChar).Value = dwData["QCKFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
